package com.buaa.divinationmaster.core.database.entities;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;

/**
 * 爻辞数据实体类
 * 包含六十四卦384条爻辞的完整信息，用于梅花易数和金钱卦详细解读
 */
@Entity(tableName = "yao_texts")
public class YaoText {
    
    @PrimaryKey(autoGenerate = true)
    public long id;
    
    @ColumnInfo(name = "hexagram_id")
    public int hexagramId;            // 对应的卦象ID (1-64)
    
    @ColumnInfo(name = "yao_position")
    public int yaoPosition;           // 爻位 (1-6: 初爻、二爻、三爻、四爻、五爻、上爻)
    
    @ColumnInfo(name = "yao_type")
    public String yaoType;            // 爻性质: 阳爻(1) 或 阴爻(0)
    
    @ColumnInfo(name = "yao_text")
    public String yaoText;            // 爻辞原文
    
    @ColumnInfo(name = "yao_meaning")
    public String yaoMeaning;         // 爻辞含义解释
    
    @ColumnInfo(name = "yao_image")
    public String yaoImage;           // 爻象
    
    @ColumnInfo(name = "moving_interpretation")
    public String movingInterpretation; // 动爻解释 (当此爻为动爻时的特殊含义)
    
    @ColumnInfo(name = "element")
    public String element;            // 五行属性
    
    @ColumnInfo(name = "six_relatives")
    public String sixRelatives;       // 六亲 (父母、兄弟、妻财、子孙、官鬼)
    
    @ColumnInfo(name = "keywords")
    public String keywords;           // 关键词 (逗号分隔)
    
    // 默认构造函数
    public YaoText() {}
    
    // 带参构造函数 - Room忽略此构造函数
    @Ignore
    public YaoText(int hexagramId, int yaoPosition, String yaoType, String yaoText) {
        this.hexagramId = hexagramId;
        this.yaoPosition = yaoPosition;
        this.yaoType = yaoType;
        this.yaoText = yaoText;
    }
    
    // Getter和Setter方法
    public long getId() { return id; }
    public void setId(long id) { this.id = id; }
    
    public int getHexagramId() { return hexagramId; }
    public void setHexagramId(int hexagramId) { this.hexagramId = hexagramId; }
    
    public int getYaoPosition() { return yaoPosition; }
    public void setYaoPosition(int yaoPosition) { this.yaoPosition = yaoPosition; }
    
    public String getYaoType() { return yaoType; }
    public void setYaoType(String yaoType) { this.yaoType = yaoType; }
    
    public String getYaoText() { return yaoText; }
    public void setYaoText(String yaoText) { this.yaoText = yaoText; }
    
    public String getYaoMeaning() { return yaoMeaning; }
    public void setYaoMeaning(String yaoMeaning) { this.yaoMeaning = yaoMeaning; }
    
    public String getYaoImage() { return yaoImage; }
    public void setYaoImage(String yaoImage) { this.yaoImage = yaoImage; }
    
    public String getMovingInterpretation() { return movingInterpretation; }
    public void setMovingInterpretation(String movingInterpretation) { this.movingInterpretation = movingInterpretation; }
    
    public String getElement() { return element; }
    public void setElement(String element) { this.element = element; }
    
    public String getSixRelatives() { return sixRelatives; }
    public void setSixRelatives(String sixRelatives) { this.sixRelatives = sixRelatives; }
    
    public String getKeywords() { return keywords; }
    public void setKeywords(String keywords) { this.keywords = keywords; }
    
    /**
     * 获取爻位的中文名称
     */
    public String getYaoPositionName() {
        switch (yaoPosition) {
            case 1: return "初爻";
            case 2: return "二爻";
            case 3: return "三爻";
            case 4: return "四爻";
            case 5: return "五爻";
            case 6: return "上爻";
            default: return "未知";
        }
    }
    
    /**
     * 获取爻性质的中文描述
     */
    public String getYaoTypeDescription() {
        if ("1".equals(yaoType)) {
            return "阳爻";
        } else if ("0".equals(yaoType)) {
            return "阴爻";
        }
        return "未知";
    }
    
    /**
     * 判断是否为阳爻
     */
    public boolean isYangYao() {
        return "1".equals(yaoType);
    }
    
    /**
     * 判断是否为阴爻
     */
    public boolean isYinYao() {
        return "0".equals(yaoType);
    }
    
    /**
     * 获取完整的爻位描述
     */
    public String getFullDescription() {
        return getYaoPositionName() + "：" + getYaoTypeDescription() + " - " + yaoText;
    }
    
    @Override
    public String toString() {
        return "YaoText{" +
                "id=" + id +
                ", hexagramId=" + hexagramId +
                ", yaoPosition=" + yaoPosition +
                ", yaoType='" + yaoType + '\'' +
                ", yaoText='" + yaoText + '\'' +
                '}';
    }
    
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        YaoText yaoText = (YaoText) obj;
        return id == yaoText.id;
    }
    
    @Override
    public int hashCode() {
        return Long.hashCode(id);
    }
}
